#include "Widget.h"
#include "ui_Widget.h"

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::f1()
{
   //   creamos el OID Figura
   pair<string, int> primero("OIDF1",0);
   //   añadimos los tracks relacionados a la figura
   m_vigilanceMap.insert(pair<pair<string, int>, string>(primero, "OIDT1"));
   m_vigilanceMap.insert(pair<pair<string, int>, string>(primero, "OIDT2"));
   m_vigilanceMap.insert(pair<pair<string, int>, string>(primero, "OIDT3"));

   // aquí guardamos los OID's de los tracks para esta figura
   list<string> currentList;

   multimap<pair<string, int>, string>::iterator  it;
   for (it = m_vigilanceMap.begin(); it != m_vigilanceMap.end(); it++)
   {
      pair<string, int> tmpFig   = (*it).first;
      string            tmpTrk   = (*it).second;
      cout << "tmpFig : " << tmpFig.first  << " - " << tmpFig.second << endl;
      cout << "tmpTrk : " << tmpTrk << endl;
      currentList.push_back(tmpTrk);
   }  // hemos llenado la lista con los tracks de la figura

   // para revisar la currentList
   cout << "---------- currentList : " << endl;
   readList(currentList);

   // para revisar la lastList
   cout << "---------- lastList : " << endl;
   list<string> lastList   = currentList;
   readList(lastList);

   // cambiamos el contenido por parte de los tracks
   m_vigilanceMap.clear();
   //   añadimos los tracks relacionados a la figura
   m_vigilanceMap.insert(pair<pair<string, int>, string>(primero, "OIDT1"));
   m_vigilanceMap.insert(pair<pair<string, int>, string>(primero, "OIDT3"));
   m_vigilanceMap.insert(pair<pair<string, int>, string>(primero, "OIDT5"));
   m_vigilanceMap.insert(pair<pair<string, int>, string>(primero, "OIDT6"));

   // limpiamos el contenido de currentList
   currentList.clear();

   // llenamos currentList
   for (it = m_vigilanceMap.begin(); it != m_vigilanceMap.end(); it++)
   {
      pair<string, int> tmpFig   = (*it).first;
      string            tmpTrk   = (*it).second;
      currentList.push_back(tmpTrk);
   }  // hemos llenado la lista con los tracks de la figura

   // para revisar la currentList
   cout << "---------- currentList : " << endl;
   readList(currentList);

   list<string> fusionList = currentList;
   // union de listas
   fusionList.merge(lastList);
   // al hacer un merge la lista se borra
//   fusionList.merge(currentList);
   // remuevo duplicados
   fusionList.unique();
   // para revisar la fusionList
   cout << "---------- fusionList : " << endl;
   readList(fusionList);

   cout << "---------- currentList : " << endl;
   readList(currentList);


   list<string>::iterator it2;
   list<string>::iterator it3;
   /*
   for (it2=fusionList.begin(); it2!=fusionList.end(); it2++)
   {
      bool  bRetorna = false;
      list<string>::iterator  it3;
      for (it3 = currentList.begin(); it3 != currentList.end(); it3++)  {
         cout << "it2 = " << *it2 << " - " << "it3 = " << *it3 << endl;
         if (*it3 == *it2) {
            bRetorna = true;
            break;
         }
      }
      cout << "bRetorna : " << bRetorna << endl;
   }
*/
   for (it2=fusionList.begin(); it2!=fusionList.end(); it2++) {
       it3 = std::find(currentList.begin(), currentList.end(), *it2);
       if (it3 != currentList.end()){
           cout << "1" << endl;
       } else {
           cout << "0" << endl;
       }
   }

}

void Widget::f2()
{
    int myints[] = { 10, 20, 30 ,40 };
      int * p;

      // pointer to array element:
      int x = ui->lineEdit->text().toInt();
//      int x = 10;
      cout << "&x = " << &x << " x = " << x << endl;
      p = std::find(myints,myints+4, x);
      cout << " p = " << p << " *p = " << *p << endl;
//      ++p;
//      cout << " p = " << p << " *p = " << *p << endl;
      list<int> lista;
      lista.push_back(10);
      lista.push_back(20);
      lista.push_back(30);
      lista.push_back(40);

//      vector<int> miVector(lista.begin(), lista.end());
//      vector<int>::iterator it;
//      it = std::find(miVector.begin(), miVector.end(), x);

      list<int>::iterator it2;
      it2 = std::find(lista.begin(), lista.end(), x);

      if (it2 != lista.end())
      {
          cout << "match ok" << endl;
      }
      else{
          cout << "no match" << endl;
      }
      cout << endl;

}

void Widget::f3()
{
   multimap<string, int>               mymultimap;
   multimap<string, int>::iterator     it;
   multimap<string, int>::key_compare  mycomp;
   string highest;
   mycomp   = mymultimap.key_comp();
   pair<multimap<string, int>::iterator, multimap<string, int>::iterator>   ret;

   mymultimap.insert(pair<string, int>("a",100));
   mymultimap.insert(pair<string, int>("b",200));
   mymultimap.insert(pair<string, int>("b",300));
   mymultimap.insert(pair<string, int>("b",400));
   mymultimap.insert(pair<string, int>("c",500));
   mymultimap.insert(pair<string, int>("c",600));
   mymultimap.insert(pair<string, int>("d",700));

   highest  = mymultimap.begin()->first;
   cout << "highest : " << highest << endl;
   highest  = mymultimap.rbegin()->first;
   cout << "highest : " << highest << endl;

   it = mymultimap.find("d");
   if (it != mymultimap.end())
   {
      cout << "it = " << (*it).first << " - " << (*it).second << endl;
   }
   else
   {
      cout << "NO" << endl;
   }

   ret = mymultimap.equal_range("b");
   for (it=ret.first; it!=ret.second; ++it)
      cout << " " << (*it).second << endl;

}

void Widget::f4()
{
   multimap<pair<string, int>, string> myMap;
//   pair<string, int> primero("a", 100);
   myMap.insert(pair<pair<string, int>, string>(pair<string, int>("a", 1), "x"));
   myMap.insert(pair<pair<string, int>, string>(pair<string, int>("a", 1), "y"));
   myMap.insert(pair<pair<string, int>, string>(pair<string, int>("a", 1), "z"));
   myMap.insert(pair<pair<string, int>, string>(pair<string, int>("b", 0), "x"));
   myMap.insert(pair<pair<string, int>, string>(pair<string, int>("b", 0), "z"));
   myMap.insert(pair<pair<string, int>, string>(pair<string, int>("c", 0), "x"));
   myMap.insert(pair<pair<string, int>, string>(pair<string, int>("c", 0), "y"));

   multimap<pair<string, int>, string>::iterator   it;
//   multimap<pair<string, int>, string>::iterator   it2;
   pair<multimap<pair<string, int>, string>::iterator   , multimap<pair<string, int>, string>::iterator   > ret;
   ret = myMap.equal_range(pair<string, int>("a", 1));
   for (it=ret.first; it!=ret.second; ++it){
      cout << " " << (*it).second << endl;
   }
}

void Widget::f5()
{
    map<pair<int,int>, string> listaMensajesRecibidos;
    pair <int, int> primero;
    primero.first   = 5;
    primero.second  = 7;
    string  segundo = "Ronald";
    listaMensajesRecibidos.insert(pair<pair<int,int>, string>(primero, segundo));
    cout << primero.first << ":" << primero.second << " - " << segundo << endl;
    segundo = "Ronald +";
    listaMensajesRecibidos.insert(pair<pair<int,int>, string>(primero, segundo));
    cout << primero.first << ":" << primero.second << " - " << segundo << endl;
}

bool Widget::existe(list<string> &elements, const string element)
{
   list<string>::iterator  it;
   bool  bRetorna = false;
   for (it = elements.begin(); it != elements.end(); it++)  {
      if (*it == element) {
//         bRetorna = true;
//         break;
      }
   }
   return bRetorna;
}

void    Widget::readList(std::list<string> lista)
{
    list<string>::iterator it;
    for (it = lista.begin(); it!=lista.end(); it++)
        cout << " " << *it << endl;
}

void Widget::on_pushButton_clicked()
{
//    f1();
//    f2();
//   f3();
//   f4();
    f5();
}

void Widget::on_pushButton_2_clicked()
{
    close();
}
